package yxy.game.pm2.command.union.boss;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionBossMessage;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.boss.UnionBossHandle;
import yxy.game.pm2.module.union.player.UnionPlayer;

/** 联盟BOSS 伤害排名 */
@MessageType(proto = Command.UnionBossRank, classtype = UnionBossMessage.UnionBossRankRequest.class)
public class CMD_UnionBossRank extends Command<UnionBossMessage.UnionBossRankRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionBossMessage.UnionBossRankRequest request) {
		String playerCode = messageSource.getTag();
		int bossId = request.getBossId();

		UnionBossMessage.UnionBossRankResponse.Builder response = UnionBossMessage.UnionBossRankResponse.newBuilder();

		// 检查是否已经有联盟
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			send(messageSource, response, result);
			return;
		}
		UnionBean union = entry.getUnionBean();
		UnionPlayer unionPlayer = entry.getUnionPlayer();

		result = new UnionBossHandle().bossRank(playerCode, bossId, union, unionPlayer, response);
		send(messageSource, response, result);
	}

	private void send(MessageSource messageSource, UnionBossMessage.UnionBossRankResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}